package com.cart.musicdemo.adapter;

/**
 * Copyright (c) 2024 - CARThunder
 * All Rights Reserved by Chengdu CARThunder Intelligent Technology Co., LTD and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with CARThunder in writing by applicable law.
 *
 */

/**
 * file description.
 * @file    filename
 * @brief   brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author  author_name
 * @date    last_modified_date
 *
 *      Edit History
 *      ----------------------------------------------------------------------------
 *      DATE                     NAME               DESCRIPTION
 *      2024-08-2               jia fengyi         Create it.
 *
 */

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.cart.musicdemo.R;

import com.cart.musicdemo.base.utils.StringUtils;
import com.cart.musicdemo.model.SongModel;

import java.util.List;

/**
 * The type Play list adapter.
 */
public class ArtistSongListAdapter extends RecyclerView.Adapter<ArtistSongListAdapter.ViewHolder> {


    private List<SongModel> songList;

    private OnItemClickListener listener;


    /**
     * The interface On item click listener.
     */
    public interface OnItemClickListener {
        /**
         * On item click.
         *
         * @param songModel the song model
         * @param reset     the reset
         */
        void onItemClick(SongModel songModel, boolean reset);
    }


    /**
     * Instantiates a new Play list adapter.
     *
     * @param data the data
     * @param item the item
     */
    public ArtistSongListAdapter(List<SongModel> data, OnItemClickListener item) {
        this.songList = data;
        this.listener = item;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_songs_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // 拼接两个字符串
        holder.textView.setText(
                StringUtils.subString(
                        songList.get(position).getName(), songList.get(position).getSinger()));
        holder.ivSongImage.setTag(R.id.ivSongImage, position);
        if (position == (int) holder.ivSongImage.getTag(R.id.ivSongImage)
                && songList.get(position).getPlaying()) {
            holder.ivSongImage.setImageResource(R.drawable.ic_baseline_headset_24);
        } else {
            holder.ivSongImage.setImageResource(R.drawable.ic_baseline_music_note_24);
        }
        //列表绑定监听
        holder.relativeLayout.setOnClickListener(
                v -> listener.onItemClick(songList.get(position),false));
    }

    @Override
    public int getItemCount() {
        return songList.size();
    }

    /**
     * The type View holder.
     */
    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;
        private RelativeLayout relativeLayout;
        private ImageView ivSongImage;

        /**
         * Instantiates a new View holder.
         *
         * @param itemView the item view
         */
        public ViewHolder(View itemView) {
            super(itemView);
            ivSongImage = itemView.findViewById(R.id.ivSongImage);
            relativeLayout = itemView.findViewById(R.id.rl_click);
            textView = itemView.findViewById(R.id.tvSongName);

        }
    }

}


